﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using ProtocolVN.Framework.Core;
using ProtocolVN.Framework.Win;

namespace ProtocolVN.Framework.Win
{
    public class XuLyRangBuocPhieu
    {
        static StyleFormatCondition styleFormatCondition1 = new StyleFormatCondition();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dsPhieuNguon">Là 1 DataSet phải có các cột HH_ID,SO_LUONG và DataSet này gồm 
        ///                           \n các mã hàng hóa với số lượng còn lại chưa tạo ra phiếu khác</param>
        /// <param name="gridDich">Là Lưới chứa các mã hàng hóa của phiếu đích</param>
        public static bool KiemTraRangBuoc(DataSet dsPhieuNguon, PLGridView gridDich) 
        {
            // thuc hien doi mau dong vi pham rang buoc ve so luong
            TaoDieuKienLoc(gridDich);
            bool ViPhamRangBuoc = false;
            for (int i = 0; i < gridDich.RowCount; i++)
            {
                for (int k = 0; k < dsPhieuNguon.Tables[0].Rows.Count; k++)
                {
                    DataRow rowNguon= dsPhieuNguon.Tables[0].Rows[k];
                    DataRow rowDich=gridDich.GetDataRow(i);
                    if (HelpNumber.ParseInt64(rowDich["HH_ID"]) ==  HelpNumber.ParseInt64(rowNguon["HH_ID"]))
                    {
                        if (HelpNumber.ParseDecimal(rowDich["SO_LUONG"]) > HelpNumber.ParseDecimal(rowNguon["SO_LUONG"]))
                        {
                            gridDich.SetRowCellValue(i, gridDich.Columns["CHECK"], 0);
                            ViPhamRangBuoc = true;
                        }
                        else
                            gridDich.SetRowCellValue(i, gridDich.Columns["CHECK"], 1);
                    }
                }
            }
            return ViPhamRangBuoc;
        }

        private static void TaoDieuKienLoc(PLGridView grid)
        { 
            GridColumn colDK= new GridColumn();
            colDK.FieldName="CHECK";
            colDK.Visible = false;
            DataSet ds = ((DataTable)grid.GridControl.DataSource).DataSet;
            ds.Tables[0].Columns.Add("CHECK");
            grid.Columns.Add(colDK);
            ///////////
            styleFormatCondition1.Appearance.BackColor = System.Drawing.Color.Red;//FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            styleFormatCondition1.Appearance.ForeColor = System.Drawing.Color.Blue;
            styleFormatCondition1.Appearance.Options.UseBackColor = true;
            styleFormatCondition1.Appearance.Options.UseForeColor = true;
            styleFormatCondition1.ApplyToRow = true;
            styleFormatCondition1.Column = colDK;
            styleFormatCondition1.Condition = DevExpress.XtraGrid.FormatConditionEnum.Equal;
            styleFormatCondition1.Value1 = 0;
            grid.FormatConditions.AddRange(new StyleFormatCondition[] { styleFormatCondition1 });
        }

        private static DataSet Get_SoLuongConLai(long ID_PhieuNguon, PhieuType LoaiPhieuNguon) 
        {            
            //Lấy về số lượng còn lại từ store FW_TINH_SO_LUONG_CON
            string str = "select * from FW_TINH_SO_LUONG_CON("+ID_PhieuNguon + ","+LoaiPhieuNguon.GetTypeID()+")";
            try
            {
                return DABase.getDatabase().LoadDataSet(str);
            }
            catch { return new DataSet(); }
        }

        public static bool KiemTraTaoPhieu(long ID_PhieuNguon, PhieuType LoaiPhieu)
        {
            DataSet ds = Get_SoLuongConLai(ID_PhieuNguon, LoaiPhieu);
            // Neu phieu nguon chua tao het phieu lien quan thi co the tao duoc phieu dich 
            if (ds != null && ds.Tables[0].Rows.Count > 0)
                return true;
            else 
                return false ;            
        }        

        public static DataTable ViPhamRangBuoc(long ID_PhieuNguon, PhieuType LoaiPhieuNguon,object DOPhieuDich, PhieuType LoaiPhieuDich)
        {
            DataSet dsNguon = Get_SoLuongConLai(ID_PhieuNguon, LoaiPhieuNguon);
            DataSet dsDich = new DataSet();
            DataTable dtViPham = new DataTable();
            dtViPham.Columns.Add("HH_ID", Type.GetType("Int64"));
            dtViPham.Columns.Add("VI_PHAM");
            if (LoaiPhieuDich.GetTypeID() == 1)
            {                
            }
            return new DataTable();
        }

        public static DataTable ViPhamRangBuoc(long ID_PhieuNguon, PhieuType LoaiPhieuNguon,PLGridView gridDich)
        {
            DataSet dsNguon = Get_SoLuongConLai(ID_PhieuNguon, LoaiPhieuNguon);            
            DataTable dtViPham = new DataTable();
            dtViPham.Columns.Add("HH_ID", Type.GetType("Int64"));
            dtViPham.Columns.Add("VI_PHAM");

            if (dsNguon == null || dsNguon.Tables[0].Rows.Count == 0) 
                return dtViPham;

            for (int i = 0; i < gridDich.RowCount; i++)
            {
                DataRow rowDich = gridDich.GetDataRow(i);
                DataRow rowViPham = dtViPham.NewRow();               
                bool exists = false;
                for (int k = 0; k < dsNguon.Tables[0].Rows.Count; k++)
                {
                    DataRow rowNguon = dsNguon.Tables[0].Rows[k];
                    if (HelpNumber.ParseInt64(rowDich["HH_ID"]) == HelpNumber.ParseInt64(rowNguon["HH_ID"]))
                    {
                        // HH_ID co ton tai trong phieu nguon
                        exists = true;
                        if (HelpNumber.ParseDecimal(rowDich["SO_LUONG"]) > HelpNumber.ParseDecimal(rowNguon["SO_LUONG"]))
                        {
                            rowViPham["HH_ID"] = rowDich["HH_ID"];
                            rowViPham["VI_PHAM"] = "Vi phạm về ràng buộc số lượng";
                            dtViPham.Rows.Add(rowViPham);
                        }
                    }
                }
                if (!exists)
                {
                    rowViPham["HH_ID"] = rowDich["HH_ID"];
                    rowViPham["VI_PHAM"] = "Mã hàng này không có trong phiếu nguồn";
                    dtViPham.Rows.Add(rowViPham);
                }
            }
            return dtViPham;
        }       
    }
}
